home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / jovept2.arc / META.C < prev    next >
Encoding:
C/C++ Source or Header  |  1985-05-30  |  5.0 KB  |  293 lines

  1. /* meta.c */
  2.  
  3. /* JOVE/MSDOS. K. Mitchum 1/85 */
  4. /* Modifications for personal use only. */
  5. /* original code J. Payne LSRHS 5/83 */
  6. /* Ken Mitchum */
  7. /* University of Pittsburgh */
  8. /* Decision Systems Laboratory */
  9.  
  10. /*
  11.    Jonathan Payne at Lincoln-Sudbury Regional High School 5-25-83
  12.   
  13.    jove_meta.c
  14.  
  15.    Various commands that are (by default) invoked by ESC-key.
  16.    Not a very good name since the ESC key can be rebound...  */
  17.  
  18.  
  19. #include "jove.h"
  20.  
  21. #ifdef UNIX
  22. #include <ctype.h>
  23. #endif
  24.  
  25. extern int RMargin;
  26. extern int    diffnum;
  27.  
  28.  
  29.  
  30. PrefAbort(str)
  31. char    *str;
  32. {
  33.     s_mess("%s aborted", str);
  34.     rbell();
  35. }
  36.  
  37. Unknown(pref, c)
  38. char    pref,
  39.     c;
  40. {
  41.     s_mess("%c-%c unbound", pref, c);
  42.     rbell();
  43. }
  44.  
  45. Upperc(c)
  46. register int    c;
  47. {
  48.     return (islower(c) ? toupper(c) : c);
  49. }
  50.  
  51. FourTime()
  52. {
  53.     exp_p = 1;
  54.     exp *= 4;
  55.     this_cmd = ARG_CMD;
  56. }
  57.  
  58. EscPrefix()
  59. {
  60.     register int    c, i, sign;
  61.     FUNCT *mp;
  62.     int    invokingchar = LastKeyStruck;
  63.  
  64.     c = (*Gtchar)();
  65.  
  66.     if (c == CTL(G)) {
  67.         PrefAbort("Prefix-1");
  68.         return;
  69.     }
  70.  
  71.     if (c == '-' || (c >= '0' && c <= '9')) {
  72.         sign = c == '-' ? -1 : 1;
  73.         if (sign == 1)
  74.             i = c - '0';
  75.         else
  76.             i = 0;
  77.         for (;;) {
  78.             c = (*Gtchar)();
  79.             if (c >= '0' && c <= '9')
  80.                 i = i * 10 + (c - '0');
  81.             else {
  82.                 exp_p = 1;
  83.                 exp = i * sign;
  84.                 peekc = c;
  85.                 this_cmd = ARG_CMD;
  86.                 return;
  87.             }
  88.         }
  89.     }
  90.  
  91.     mp = pref1map[c];
  92.     if (mp == 0)
  93.         Unknown(invokingchar, c);
  94.     ExecFunc(mp, 0);
  95. }
  96.  
  97. /*
  98.  * Save a region.  A pretend kill.
  99.  */
  100.  
  101. CopyRegion()
  102. {
  103.     LINE    *nl;
  104.     MARK    *mp;
  105.     int    status,
  106.         mod = curbuf->b_modified;
  107.  
  108.     mp = CurMark();
  109.  
  110.     if (mp->m_line == curline && mp->m_char == curchar)
  111.         complain((char *) 0);
  112.  
  113.     killptr = ((killptr + 1) % NUMKILLS);
  114.     if (killbuf[killptr])
  115.         lfreelist(killbuf[killptr]);
  116.     nl = killbuf[killptr] = nbufline();
  117.     nl->l_dline = putline("");
  118.     nl->l_next = nl->l_prev = 0;
  119.  
  120.     status = inorder(mp->m_line, mp->m_char, curline, curchar);
  121.     if (status == -1)
  122.         return;
  123.  
  124.     if (status)
  125.         ignore(DoYank(mp->m_line, mp->m_char, curline, curchar,
  126.                 nl, 0, (BUFFER *) 0));
  127.     else
  128.         ignore(DoYank(curline, curchar, mp->m_line, mp->m_char,
  129.                 nl, 0, (BUFFER *) 0));
  130.     curbuf->b_modified = mod;
  131. }
  132.  
  133. DelNWord()
  134. {
  135.     dword(1);
  136. }
  137.  
  138. DelPWord()
  139. {
  140.     dword(0);
  141. }
  142.  
  143. dword(forward)
  144. {
  145.     BUFLOC    savedot;
  146.  
  147.     DOTsave(&savedot);
  148.     forward ? ForWord() : BackWord();
  149.     reg_kill(savedot.p_line, savedot.p_char, curline, curchar, !forward);
  150. }
  151.  
  152. UppWord()
  153. {
  154.     case_word(1);
  155. }
  156.  
  157. LowWord()
  158. {
  159.     case_word(0);
  160. }
  161.  
  162. ToIndent()
  163. {
  164.     register char    *cp, c;
  165.  
  166.     for (cp = linebuf; c = *cp; cp++)
  167.         if (c != ' ' && c != '\t')
  168.             break;
  169.     curchar = cp - linebuf;
  170. }
  171.  
  172. GoLine()
  173. {
  174.     if (exp_p == 0)
  175.         return;
  176.     SetLine(next_line(curbuf->b_zero, exp - 1));
  177. }
  178.  
  179. VtKeys()
  180. {
  181.     int    c = getch();
  182.  
  183.     switch (c) {
  184.     case 'A':
  185.         PrevLine();
  186.         break;
  187.  
  188.     case 'B':
  189.         NextLine();
  190.         break;
  191.  
  192.     case 'D':
  193.         BackChar();
  194.         break;
  195.  
  196.     case 'C':
  197.         ForChar();
  198.         break;
  199.  
  200.     default:
  201.         complain("Unknown command ESC-[-%c", c);
  202.     }
  203. }
  204.  
  205.  
  206.  
  207. Justify()
  208. {
  209.     BUFLOC    *bp;
  210.     LINE    *start,
  211.         *end;
  212.     char    *ParaStr = "^\\.\\|^$";
  213.  
  214.     bp = dosearch(ParaStr, -1, 1);    /* Beginning of paragraph */
  215.     if (bp)        /* Not the tab case */
  216.         start = bp->p_line->l_next;
  217.     else
  218.         start = curbuf->b_zero;
  219.     bp = dosearch(ParaStr, 1, 1);        /* End of paragraph */
  220.     if (bp)
  221.         end = bp->p_line;
  222.     else
  223.         end = curbuf->b_dol;
  224.     DoJustify(start, end);
  225. }
  226.  
  227.  
  228.  
  229. DoJustify(l1, l2)        /* Justify text */
  230. LINE    *l1,
  231.     *l2;
  232. {
  233.     int    d1 = 0,
  234.         d2 = 1,
  235.         goal,
  236.         nlines,            /* Number of lines to justify
  237.                      * so we know when we are done.
  238.                      */
  239.         curcol;
  240.     char    *cp;
  241.     MARK    *savedot = MakeMark(curline, curchar);
  242.  
  243.     fixorder(&l1, &d1, &l2, &d2);    /* l1/c1 will be before l2/c2 */
  244.     nlines = diffnum;
  245.     SetLine(l1);
  246.  
  247.     if (lastp(l2) && !blnkp(getline(l2->l_dline, genbuf)))
  248.         nlines++;
  249.  
  250.     while (nlines) {    /* One line at a time */
  251.         goal = RMargin - (RMargin / 10);
  252.         Eol();    /* End of line */
  253.         if ((curcol = calc_pos(linebuf, curchar)) == 0) {
  254.             SetLine(curline->l_next);    /* Skip blank lines */
  255.             nlines--;
  256.         } else if (curcol < goal) {
  257.             if (nlines == 1)
  258.                 break;
  259.             DelWtSpace();    /* No white space at the end of the line */
  260.             DelNChar();    /* Delete carraige return */
  261.             nlines--;
  262.             Insertc(' ');
  263.         } else if (curcol > RMargin) {
  264.             do {
  265.                 /* curchar - 2 because curchar is the null,
  266.                  * curchar - 1 is the space (if there is
  267.                  * one there.
  268.                  */
  269.                 cp = StrIndex(-1, linebuf, curchar - 2, ' ');
  270.                 if (cp == 0)
  271.                     break;
  272.                 curchar = cp - linebuf;
  273.                 curcol = calc_pos(linebuf, curchar);
  274.             } while (curcol > RMargin);
  275.             if (cp) {
  276.                 DelWtSpace();
  277.                 LineInsert();
  278.             } else {
  279.                 SetLine(curline->l_next);
  280.                 nlines--;
  281.             }
  282.         } else {
  283.             SetLine(curline->l_next);
  284.             nlines--;
  285.         }
  286.     }
  287.     ToMark(savedot);    /* Back to where we were */
  288.     DelMark(savedot);    /* Free up the mark */
  289.     this_cmd = 0;        /* So everything is under control */
  290. }
  291.  
  292. /* end */
  293.